package cn.fl.project.controller;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.api.IPrjPaySchApiService;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.constant.PrjPrdBscInfoConstant;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjPrdBscInfoService;
import cn.fl.project.service.IVehicleSrcService;
import cn.fl.project.service.IVehicleStyleService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjPrdBscInfoVO;
import cn.fl.project.vo.PrjPrdParamConfigVO;
import com.alibaba.fastjson.JSON;
import cvm.store.facade.cvm.dto.VechileStyleDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import veh.src.facade.src.src.condition.VehSrcInfQC;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 车源库
 */
@Controller
@RequestMapping("/project/vehicleSrc")
public class VehicleSrcController {

    @Autowired
    private IVehicleSrcService vehicleSrcService;
    @Autowired
    private IPrjBscInfoService prjBscInfoService;
    @Autowired
    private IPrjPrdBscInfoService     prjPrdBscInfoService;
    @Autowired
    private IVehicleStyleService     vehicleStyleService;
    @Autowired
    private IPrjPaySchApiService prjPaySchApiService;

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(VehicleSrcController.class);

    /**
     * 车源列表页面
     * @param view
     * @param prjId 项目id
     * @return
     */
    @RequestMapping("vehicleSrcList.do")
    public ModelAndView toListPage(ModelAndView view ,Long prjId){
        view.setViewName("project/vehicle/vehicleSrcList");
        view.addObject("prjId",prjId);
        return view;
    }

    /**
     * 车源查询
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("initList.do")
    @ResponseBody
    public Object initList(HttpServletRequest request, HttpServletResponse response){
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pageSize");
        String carNo = request.getParameter("mhCarNo");//车源编号
        String prjId = request.getParameter("prjId");

        Pager pager = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            pager.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            pager.setEveryPage(Integer.parseInt(pageSize));
        }
        PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
        VehSrcInfQC  qc = new VehSrcInfQC();
        if(StringUtils.isNotBlank(carNo)){
            qc.setMhCarNo(carNo);
        }
        List<Long> statusasList =  new ArrayList<Long>();//车源检测状态
        statusasList.add(PrjPrdBscInfoConstant.PRD_ASSESSMENT_STATE_IN);
        statusasList.add(PrjPrdBscInfoConstant.PRD_ASSESSMENT_STATE_PASS);
        statusasList.add(PrjPrdBscInfoConstant.PRD_ASSESSMENT_STATE_ONSHELVES);
        qc.setStatusas(statusasList);

        String bizType =  PrjBscInfoConstant.getBusinessTypeMap.get(prjBscInfoVO.getBuOrgCd()+prjBscInfoVO.getPrjTypCd());
        qc.setBusinessType(bizType);

        List<Long> deteStatesList =  new ArrayList<Long>();//车源评估审核状态
        deteStatesList.add(PrjPrdBscInfoConstant.PRD_CHECK_STATE_IN);
        deteStatesList.add(PrjPrdBscInfoConstant.PRD_CHECK_STATE_PASS);
        qc.setDeteStates(deteStatesList);

        pager.setCondition(qc);
        pager = vehicleSrcService.selectListPage(pager);
        List<VechileStyleDTO> resultList = (List<VechileStyleDTO>) pager.getResultList();
        ListPageVO<VechileStyleDTO> listPage = new ListPageVO<VechileStyleDTO>(resultList, pager.getTotalCount());
        return listPage;
    }



    /**
     * 预筛选车源查询
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("getVehSourceList.do")
    @ResponseBody
    public Object getVehSourceList(HttpServletRequest request, HttpServletResponse response){
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String carNo = request.getParameter("carNo");//车源编号
        String prjTypCd = request.getParameter("prjTypCd") ; // 项目类型

        BaseLogUtils.newLogger("预筛选车源查询###carNo:" + carNo + ",prjTypCd:" + prjTypCd).info();
        Pager pager = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            pager.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            pager.setEveryPage(Integer.parseInt(pageSize));
        }
        VehSrcInfQC  qc = new VehSrcInfQC();
        if(StringUtils.isNotBlank(carNo)){
            qc.setMhCarNo(carNo);
        }

        String bizType =  PrjBscInfoConstant.getBusinessTypeMap.get(CurrentThreadContext.getCurrentBuOrgCd()+prjTypCd);
        qc.setBusinessType(bizType);

        List<Long> deteStatesList =  new ArrayList<Long>();//车源评估审核状态
        deteStatesList.add(PrjPrdBscInfoConstant.PRD_CHECK_STATE_PASS);
        qc.setDeteStates(deteStatesList);

        pager.setCondition(qc);
        BaseLogUtils.newLogger("查询车源库###qc:" + JSON.toJSONString(qc)) ;
        pager = vehicleSrcService.selectListPage(pager);
        BaseLogUtils.newLogger("查询车源库###pager:" + JSON.toJSONString(pager)) ;
        List<VechileStyleDTO> resultList = (List<VechileStyleDTO>) pager.getResultList();
        ListPageVO<VechileStyleDTO> listPage = new ListPageVO<VechileStyleDTO>(resultList, pager.getTotalCount());
        return listPage;
    }

 /*   @RequestMapping("getPrjPrdBscInfoAndExtById.do")
    @ResponseBody
    public PrjPrdBscInfoVO getPrjPrdBscInfoAndExtById(String id){
        PrjPrdBscInfoVO res = this.vehicleSrcService.getPrjPrdBscnInfoAndExtById(id);
        return res;
    }*/
    /**
     *
     * @param id 车型id
     * @return
     */
    @RequestMapping("carDetail.do")
    @ResponseBody
    public PrjPrdParamConfigVO carDetail(Long id,String prjTypCd,String buOrgCd){
        // 查询车型具体参数信息
        PrjPrdParamConfigVO prdDetail = this.vehicleSrcService.getPrdDetail(id,prjTypCd,buOrgCd);
        return prdDetail;
    }

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/vehicleSrcList.do", method = { RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("project/vehicle/vehicleSrcList");
        String prjId = request.getParameter("prjId");
        view.addObject("prjId",prjId);
        BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }


    @RequestMapping("getPrjPrdBscInfoAndExtById.do")
    @ResponseBody
    public PrjPrdBscInfoVO getPrjPrdBscInfoAndExtById(String id){
        PrjPrdBscInfoVO res = this.vehicleStyleService.getPrjPrdBscnInfoAndExtById(id);
        return res;
    }
}
